import {
  store,
  utils,
  cacheKeys,
  log,
  config
} from '@/common/app/index.js';
import themeService from './theme'

export default {
  /**
   * 获取IP地址功能
   */
  getClientIp() {
    setTimeout(() => {
      if (window.returnCitySN && window.returnCitySN.cip) {
        log.debug('获取客户端IP地址成功，当前客户端IP地址为：', window.returnCitySN.cip);
        store.commit(cacheKeys.CACHE_CLIENT_ADDRESS, window.returnCitySN.cip);
      }
    }, 1000);
  },

  /**
   * 初始化语种消息
   */
  initLanguage() {
    let currentLanguage = store.getters.currentLanguage;
    this.initLanguageList();
    this.switchLanguage(currentLanguage);
  },
  
  /**
   * 将配置的主题封装成主题列表保存在缓存中
   */
  initLanguageList() {
    let languageList = [];
    let languageConfig = config.LANGUAGE_CONFIG || {};
    for (let key in languageConfig) {
      languageList.push({
        label: utils.lang(languageConfig[key].name),
        value: key
      });
    }
    store.commit(cacheKeys.CACHE_LANGUAGE_LIST, languageList);
  },

  /**
   * 初始化主题消息
   */
  initTheme() {
    let currentTheme = store.getters.currentTheme;
    this.initThemeList();
    this.switchTheme(currentTheme);
  },
  
  /**
   * 将配置的主题封装成主题列表保存在缓存中
   */
  initThemeList() {
    let themeList = [];
    let themeConfig = config.THEME_CONFIG || {};
    for (let key in themeConfig) {
      themeList.push({
        label: utils.lang(themeConfig[key].name),
        value: key
      });
    }
    store.commit(cacheKeys.CACHE_THEME_LIST, themeList);
  },

  /**
   * 切换App主题信息
   */
  switchTheme(theme) {
    themeService.updateTheme(theme, store.getters.currentTheme);
    store.commit(cacheKeys.CACHE_CURRENT_THEME, theme);
  },

  /**
   * 切换App语种信息
   */
  switchLanguage(language) {
    // 先设置语种，然后再根据语种获取展示文本
    store.commit(cacheKeys.CACHE_CURRENT_LANGUAGE, language);
    document.title = utils.lang('system.platform.name');
    // 语种切换完成后，将缓存中的主题列表配置也重新生成，需要根据切换后的语种重新生成主题名称
    if (window.systemConfig && window.systemConfig.THEME_SWITCH_ABLE) {
      this.initThemeList();
    }
  }
}
